package com.github.vortexdev.yachtclub.controllers;

import com.github.vortexdev.yachtclub.controllers.exceptions.YachtNotFoundException;
import com.github.vortexdev.yachtclub.jsonmodels.JqGridTableRows;
import com.github.vortexdev.yachtclub.models.Yacht;
import com.github.vortexdev.yachtclub.services.RegattaService;
import com.github.vortexdev.yachtclub.services.YachtService;
import com.github.vortexdev.yachtclub.util.db.Page;
import com.github.vortexdev.yachtclub.util.db.Sort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Sergey Parakhin (sergey.parahin@gmail.com)
 */

@Controller
public class YachtController {
    private static final int DEFAULT_PAGE_SIZE = 15;
    private static String[] yachtFilteredCols = {"name", "drive", "hull", "purpose"};

    @Autowired
    YachtService yachtService;

    @Autowired
    RegattaService regattaService;

    @RequestMapping(value = "/yacht/{yachtId}", method = RequestMethod.GET)
    public String yachtPage(@PathVariable int yachtId, Model model) {
        Yacht yacht = yachtService.getYachtWithOwnerName(yachtId);
        if (yacht == null)
            throw new YachtNotFoundException("Yacht with id " + yachtId + " not found.");

        model.addAttribute("yacht", yacht);
        model.addAttribute("regattas", regattaService.getRegattasByYacht(yacht));
        return "yacht";
    }

    @RequestMapping(value = "/api/json/yachts", method = RequestMethod.GET)
    @ResponseBody
    public JqGridTableRows getYachtsJSON(HttpServletRequest request) {
        final boolean isSearch = Boolean.valueOf(request.getParameter("_search"));
        final boolean isSort = request.getParameter("sord") != null;
        final Page page = new Page(
                request.getParameter("rows") != null ?
                        Integer.valueOf(request.getParameter("rows")) : DEFAULT_PAGE_SIZE,
                request.getParameter("page") != null ?
                        Integer.valueOf(request.getParameter("page")) : 1
        );

        Sort sort = null;
        Map<String, Object> criteria = null;
        List<Yacht> yachts;

        if (isSort)
            sort = new Sort(request.getParameter("sord"), "yachts." + request.getParameter("sidx"));

        if (isSearch) {
            criteria = new HashMap<String, Object>();
            for (String col : yachtFilteredCols)
                if (request.getParameter(col) != null)
                    criteria.put("yachts." + col, request.getParameter(col));
        }

        if (isSearch && isSort)
            yachts = yachtService.getYachtsWithOwnerNames(page, criteria, sort);
        else if (isSearch)
            yachts = yachtService.getYachtsWithOwnerNames(page, criteria);
        else if (isSort)
            yachts = yachtService.getYachtsWithOwnerNames(page, sort);
        else
            yachts = yachtService.getYachtsWithOwnerNames(page);

        int countYachts = yachtService.countYachts();

        // create JSON response for jqGrid (see JqGridTableRows Class javadoc)
        return new JqGridTableRows<Yacht>(
                yachts.toArray(new Yacht[yachts.size()]), page.page,
                countYachts, countYachts / page.pageSize + 1
        );
    }
}
